<?php 
	//引入配置文件 
	// require_once '../config.php';
	require_once '../function.php';

	// 启用session
	 // session_start();

	function login(){
		if (empty($_POST['username'])) {
			$GLOBALS['message'] = '请输入用户名!';
			return;
		}

		if (empty($_POST['password'])) {
			$GLOBALS['message'] = '请输入密码!';
			return;
		}

		$username = $_POST['username'];
		$password = $_POST['password'];

		$query = xiu_excute_db("select * from users where username = '{$username}' limit 1;");

		// 从结果集中取一行作为关联数组
		$user = mysqli_fetch_assoc($query);

		if (!$user) {
			$GLOBALS['message'] = '用户不存在!';
			return;
		}

		if ($user['username'] !== $username) {
			$GLOBALS['message'] = '用户名或密码错误!';
			return;
		}

		if ($user['password'] !== $password) {
			$GLOBALS['message'] = '用户名或密码错误!';
			return;
		}
		if ($user['status'] === '0') {
			$GLOBALS['message'] = '该账户已被禁用，请联系系统管理员';
			return;
		}

		$_SESSION['login_user'] = $user;

		header('Location: /admin/index.php');

	}

	if ($_SERVER['REQUEST_METHOD'] === 'POST') {
		login();
	}

	if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'logout') {
		unset($_SESSION['login_user']);
		echo "string";
	}

 ?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>欢迎登陆</title>
	<link rel="stylesheet" type="text/css" href="/static/vendors/bootstrap4.6/css/bootstrap.css">
	<link rel="stylesheet" type="text/css" href="/static/vendors/animate/animate.css">
	<link rel="stylesheet" type="text/css" href="/static/css/admin.css">
	<style type="text/css">
		body{
			background-color: #2f4050;
		}
		.login{
			width: 380px;
			/*height: 300px;*/
			background-color: #fff;
			margin: 180px auto;
			border-radius: 8px;
			position: relative;
			padding-top: 100px;
			padding-left: 20px;
			padding-right: 20px;
			padding-bottom: 20px;
		}
		.login>img{
			width: 140px;
			height: 140px;
			position: absolute;
			left: 50%;
			top: -70px;
			margin-left: -70px;
			border-radius: 50%;
			border: 8px solid #fff;
			box-shadow: 0px 5px 10px #ccc;
		}
	</style>
</head>
<body>
	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
		<div class="login">
			<img src="/static/uploads/avatar/default.png" class="login_avatar">
			<?php if (!empty($message)): ?>
				<div class="form-group animated shake">
					<div class="alert alert-danger" role="alert">
						<?php echo $message; ?>
					</div>
				</div>
			<?php endif ?>
			<div class="form-group">
				<input type="text" class="form-control" id="username" name="username" placeholder="用户名" autocomplete="off" autofocus="autofocus" value="<?php echo isset($_POST['username'])? $_POST['username'] : ''; ?>">
			</div>
			<div class="form-group">
				<input type="password" class="form-control" id="password" name="password" placeholder="密码">	
			</div>
			<button type="submit" class="btn btn-secondary btn-block">登陆</button>
			
		</div>
		
	</form>
	<script src="/static/vendors/jQuery.js"></script>
	<script type="text/javascript">
		$(function(){
			// 1.单独作用域
			// 2.确保在页面加载完成后执行
			//当用户输入用户名后,显示用户头像
			$('#username').blur(function(){
				var username = $('#username').val();
				
				$.get('/admin/api/avatar.php',{'username': username}, function(data){
					console.log(data);
					if (!data){
						data = '/static/uploads/avatar/default.png';
					} 
					$('.login_avatar').attr('src', data);
				});
			})
		})
		
		
	</script>
</body>
</html>